<?php
 	/*数据库的增删查改类*/
	namespace KJ;
	use KJ\Db;
	use KJ\Kj;
	class ActiveRecord{
		public $DAO;
		public $table;
		public $where;
		public $order;
		public $limit;
		public $field;
		public $attributes;
		public function __construct(){
			$this->attributes = (object)null;		/*将属性强行转化为对象*/
			$this->DAO = Db :: getInstance(Kj :: $app -> config);		 /*链接数据库*/
			$table = get_class($this);		/*get_class获取命名空间类名*/
			$table = strrchr($table, '\\');  /*strrchr截取字符串*/
			$this->table = strtolower(Kj :: $app -> config['prefix'].substr($table, 1));
		}
		public function save(){
			$table = $this->table;
			$field = '';
			$value = '';
			if(empty($this->attributes->id)){
				foreach($this->attributes as $k => $v){
					$field .= "$k,";
					$value .= "'$v',";
				}
				$field = rtrim($field,',');
				$value = rtrim($value,',');
				$sql = "insert into $table($field) values($value)";
				$rs = $this->DAO->query($sql);/*向数据库插入数据*/
				return $rs;
			}else{
				foreach($this->attributes as $k => $v){
					if($k == 'id'){
						$id = $v;
					}else{
						$value .= "$k = '$v',";
					}
				}
				$value = rtrim($value,',');
				$sql = "update $table set $value where(id=$id)";
				$rs = $this->DAO->query($sql);/*更新数据库的数据*/
				return $rs;
			}
			
		}
		public function delete(){		/*删除数据*/
			$table = $this->table;
			$id = $this->attributes->id;
			$sql = "delete from $table where(id=$id)";
			$rs = $this->DAO->query($sql);
			return $rs;
		}
		public function find($id = ''){		/*查找所有数据*/
			$table = $this->table;
			if(empty($id)){
				$sql = "select * from $table";
				$result = $this->DAO->query($sql);
				$row = array();
				$rows = array();
				while($row = mysql_fetch_assoc($result)){
					$rows[] = $row;
				}
				return $rows;
			}else{
				$sql = "select * from $table where(id=$id)";
				$result = $this->DAO->query($sql);
				return mysql_fetch_assoc($result);
			}
		}
		
		public function select(){		/*根据条件查找数据*/
			$table = $this->table;
			$where = empty($this->where) ? '' : "where($this->where)";
			$order = empty($this->order) ? '' : 'order by '.$this->order;
			$field = empty($this->field) ? '*' : $this->field;
			$limit = empty($this->limit) ? '' : "limit $this->limit";
			$sql = "select $field from $table $where $order $limit";
			$result = $this->DAO->query($sql);
			$row = array();
			$rows = array();
			while($row = mysql_fetch_assoc($result)){
				$rows[] = $row;
			}
			return $rows;
		}
		public function where($where = ''){		/*查找数据的条件*/
			$this->where = $where;
			return $this;
		}
		public function order($order = ''){ 	/*按条件将查找出来的数据按升序或降序排列*/
			$this->order = $order;
			return $this;
		}
		public function limit($limit = ''){		/*按条件查找第几条到第几条数据*/
			$this->limit = $limit;
			return $this;
		}
		public function field($field = ''){		/*查找多少数据*全部或指定的数据*/
			$this->field = $field;
			return $this;
		}
		public function findModel($id){
			$table = $this->table;
			$sql = "select * from $table where(id=$id)";
			$result = $this->DAO->query($sql);
			$result = mysql_fetch_assoc($result);
			foreach($result as $k => $v){
				$this->attributes->$k = $v;
			}
			return $this;
		}
		
		public function hasOne($tableName , $id , $cat_id , $value = ''){
			$table = $this->table;
			
			$sql = "select * from $table inner join $tableName on $cat_id = $id where($id = $value)";
			
			$result = $this->DAO->query($sql);
		
			return mysql_fetch_assoc($result);
		}
		
		public function hasMany($tableName , $id , $cat_id , $value = ''){
			$table = $this->table;
			
			$sql = "select * from $table inner join $tableName on $cat_id = $id where($id = $value)";
			
			$result = $this->DAO->query($sql);
			
			$row = array();
			$rows = array();
			while($row = mysql_fetch_assoc($result)){
				$rows[] = $row;
			}
			return $rows;
		}
		
		public function join($tableName , $id , $cat_id ){		/*取出表中$cat_id = $id的数据*/
			$table = $this->table;
			
			$sql = "select * from $table inner join $tableName on $cat_id = $id";
			
			$result = $this->DAO->query($sql);
			
			$row = array();
			$rows = array();
			while($row = mysql_fetch_assoc($result)){
				$rows[] = $row;
			}
			return $rows;
		}		
	}
?>